<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          row-key="orderByNumber"
          :cell-class-name="cellClassName"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list, exportList } from '@/api/equipmentManage/equipmentStatement/sparePartsInventory'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '备件消耗清单',
      pageExportId: '备件消耗清单导出',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: ''
      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableData2: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'formTypeLabel', label: '消耗类型', canOrder: false, isRegular: true },
        { prop: 'outTime', label: '消耗时间', minWidth: '140', isRegular: true },
        { prop: 'deviceInfo', label: '设备信息', minWidth: '200', canOrder: false, isRegular: true },
        { prop: 'parentFormCode', label: '关联表单', isRegular: true },
        { prop: 'outByName', label: '操作人员', canOrder: false, isRegular: true },
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称', canOrder: false },
        { prop: 'spareModel', label: '规格型号', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'number', label: '数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false }
      ],
      defaultExport: [],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'outTime', label: '消耗时间', minWidth: '140', isRegular: true },
        { prop: 'formTypeLabel', label: '消耗类型', canOrder: false, isRegular: true },
        { prop: 'deviceInfo', label: '设备信息', minWidth: '200', canOrder: false, isRegular: true },
        { prop: 'parentFormCode', label: '关联表单', isRegular: true },
        { prop: 'outByName', label: '操作人员', canOrder: false, isRegular: true },
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称', canOrder: false },
        { prop: 'spareModel', label: '规格型号', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'number', label: '数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'code', label: '备件编码', type: 'input' },
        // { prop: 'name', label: '备件名称', type: 'input' },
        // { prop: 'model', label: '规格型号', type: 'input', isShow: false },
        { prop: 'searchTime', label: '消耗时间', type: 'time', hasDefault: true, defaultKey: 'defaultSearchTime' },
        { prop: 'formTypeLabel', label: '消耗类型', type: 'select', optionsName: 'formTypeLabelList' },
        { prop: 'deviceCodeName', label: '设备信息', type: 'select', optionsName: 'deviceInfoList' }
        // { prop: 'outByName', label: '操作人员', type: 'input' }
      ],
      formTypeLabelList: [
        { label: '全部', value: undefined },
        { label: '保养出库', value: '保养出库' },
        { label: '维修出库', value: '维修出库' }
      ],
      deviceInfoList: [],
      operationData: [ // 右上角的操作按钮
        // { label: '新增', perm: '' },
        // { label: '批量删除', perm: '' },
        { label: '批量导出3', perm: '' },
        { label: '页面配置2', perm: '' }
      ],
      statusOptions: [],
      defaultSearchTime: [],
      statusOptionsWarehouse: []
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  watch: {
    tableData() {
      this.$nextTick(() => {
        for (const elementNodeListOfElement of document.querySelectorAll('.SaleOrderCodeColumnClass')) {
          console.log(elementNodeListOfElement.childNodes[0])
          if (!elementNodeListOfElement.childNodes[0].childNodes[0].className || elementNodeListOfElement.childNodes[0].childNodes[0].className == 'commonTableColumn') {
            const paddingNode = document.createElement('div')
            paddingNode.className = 'SaleOrderCodeColumnClass_expand'
            paddingNode.innerHTML = ''
            elementNodeListOfElement.childNodes[0].insertBefore(paddingNode, elementNodeListOfElement.childNodes[0].childNodes[0])
          }
        }
      })
    }
  },
  created() {
    this.infoList()
    this.defaultSearchTime = [this.$moment(new Date()).subtract(1, 'months').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  mounted() {
    setTimeout(() => {
      this.$forceUpdate()
      this.$refs.table.doLayout()
    }, 200)
  },
  methods: {
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 1) {
        return 'SaleOrderCodeColumnClass'
      }
      if (!row.isNotselected) {
        return 'displayNoneClass'
      }
    },
    tableSelectable(row, index) {
      if (row.isNotselected) {
        return true
      } else {
        return false
      }
    },
    getList() {
      list(this.pageData).then(res => {
        res.data.forEach((item) => { item.isNotselected = true })
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    infoList() {
      getEquipmentList1({ pageNum: 1, pageSize: 9999 }).then(res => {
        res.data.forEach(ele => {
          ele.label = ele.code + '/' + ele.name
          ele.value = ele.code + '/' + ele.name
        })
        this.deviceInfoList = res.data
      })
    },
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        exportList(data).then(res => {
          resolve({ data: res.data })
        //   this.tableData = res.data.dataTable.data
        })
      })
    },
    getSummaries(param) {
      const {
        columns,
        data
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        if (column.property == 'number') {
          const values = data.map(item => item.number)
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      })
      return sums
    }

  }
}
</script>

<style lang="less" scoped>
/deep/.el-tooltip{
  display: flex !important;
}
/deep/ .el-table__fixed-footer-wrapper tr :last-child{
  top: 41px;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
// /deep/ .el-table .el-table__fixed{
//   height: auto !important;
//   bottom: 27px !important;
// }
/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block;
  width: 20px;
  line-height: 20px;
  height: 20px;
  text-align: center;
  margin-right: 3px;
}
/deep/.el-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}
</style>
